package design_patterns.tx;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
    public int[] twoSum(int[] nums, int target) {
        //最简单的是使用双层的for循环来实现

        //利用哈希表 存储已经遍历的  如果contains 也是o1的时间复杂度
        // 这样就可以实现 on的遍历方式
        Map<Integer,Integer> map=new HashMap<>();
        int n=nums.length;
        for (int i = 0; i <n ; i++) {
            if (map.containsKey(nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }
            map.put(target-nums[i],i);
        }
        return null;
    }
    public int lengthOfLIS(int[] nums) {
        int n=nums.length;
        int dp[]=new int[n];
        int maxLen=0;
        for (int i = 0; i <n ; i++) {
            for (int j = 0; j <i ; j++) {
                if (nums[i]>nums[j]){
                    dp[i]=Math.max(dp[i],dp[j]+1);
                    maxLen=Math.max(maxLen,dp[i]);
                }
            }
        }
        return maxLen;
    }
    //无重复最长字串
    public int lengthOfLongestSubstring(String s) {
        int i=0,j=0;
        HashSet<Character> set=new HashSet<>();
        int n=s.length();
        int maxLen=0;
        while (j<n){
            char c=s.charAt(j);
            while (set.contains(c)){
                set.remove(s.charAt(i));
                i++;
            }
            set.add(c);
            maxLen=Math.max(maxLen,j-i+1);
            j++;
        }
        return maxLen;
    }
    //最少的花费
    public int minCostClimbingStairs(int[] cost) {
        int n=cost.length;
        int [] dp=new  int[n+1];
        dp[0]=0;
        dp[1]=0;
        for (int i = 2; i <=n; i++) {
              dp[i]=Math.max(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[n];
    }

    public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, ClassNotFoundException, ExecutionException, InterruptedException {
        //拿到字节码对象
//        Class<Person> personClass = Person.class;
//        Class<?> aClass = Class.forName("design_patterns.tx.Person");
//        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
//        Class<Person> personClass = (Class<Person>) classLoader.loadClass("design_patterns.tx.Person");
//
//        Person person = (Person) personClass.newInstance();
//
//        Field name = personClass.getDeclaredField("name");
//        name.setAccessible(true);
//        Object o = name.get(person);
//        name.set(person,"lisi");
//        //获得方法
//        Method dance = personClass.getMethod("dance", String.class);
//        //通过invoke来激活方法
//        dance.invoke(person,name.get(person));
//         //获取所有的注解方法
//        Annotation[] annotation = personClass.getAnnotations();

        //创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Future<?> future = executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(10/0);
            }
        });
        System.out.println(future.get());
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("bbb");
            }
        });
    }
}
